﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace game
{
    public class ExplSmokePSystem : BillboardParticleSystem
    {
        public class Factory : FactoryPool<ExplSmokePSystem>
        {
            public Factory(Scene scene, int initialSize) : base(scene, initialSize) { }

            public override ExplSmokePSystem CreateObject()
            {
                ExplSmokePSystem ep = new ExplSmokePSystem(scene);
                ep.Initialize();
                return ep;
            }
        }

        public override bool IsAlive
        {
            get
            {
                return elapsedSystemTimeMs < MaxLifeTimeMs;
            }
        }

        protected Vector3 nextBangPos = new Vector3(0, 0, 0);
        protected float nextBangRadius = 10;

        public ExplSmokePSystem(Scene scene)
            : base(scene, Vector3.Zero)
        {
        }

        protected override void InitParameters()
        {
            textureName = "Textures\\explosions\\smoke2";
            effectName = "Effects\\BillboardParticle";
            effectTechName = "DynamicBillboardParticleReplaceBlending";

            Acceleration = 0.0f;
            maxParticles = 5;

            emitter = null;

            MinInitialSpeed = 0.0f;
            MaxInitialSpeed = 1.0f;
            MinInitialSize = 4000;
            MaxInitialSize = 5000;
            MinLifeTimeMs = 3500;
            MaxLifeTimeMs = 4500;

            color = Color.White;
        }

        public void Bang(Vector3 position, float particleSize, float radius,
            float particleSpeed, int particlesToUse)
        {
            nextBangPos = position;

            MaxInitialSize = particleSize;
            MinInitialSize = particleSize * 0.5f;

            nextBangRadius = radius;

            MaxInitialSpeed = particleSpeed;
            MinInitialSpeed = particleSpeed * 0.5f;

            SetParticles(particlesToUse);
        }


        protected override void InitParticle(int index, Vector3 position, float initialTimeMsOffset)
        {
            base.InitParticle(index, Vector3.Zero, initialTimeMsOffset);

            vertices[index].InitialPosition = nextBangPos + new Vector3(
                RandomHelper.RandomBetween(-nextBangRadius, nextBangRadius),
                RandomHelper.RandomBetween(-nextBangRadius, nextBangRadius),
                RandomHelper.RandomBetween(-nextBangRadius, nextBangRadius)
                );
            vertices[index + 1].InitialPosition = vertices[index].InitialPosition;
            vertices[index + 2].InitialPosition = vertices[index].InitialPosition;
            vertices[index + 3].InitialPosition = vertices[index].InitialPosition;

            float speed = RandomHelper.RandomBetween(MinInitialSpeed, MaxInitialSpeed);
            vertices[index].Velocity = RandomHelper.RandomVector() * speed;
            vertices[index + 1].Velocity = vertices[index].Velocity;
            vertices[index + 2].Velocity = vertices[index].Velocity;
            vertices[index + 3].Velocity = vertices[index].Velocity;
        }
    }
}
